package com.zjn.servicea.service.Imp;

import com.zjn.entity.*;
import com.zjn.servicea.dao.UserDao;

import com.zjn.servicea.service.UserService;
import com.zjn.servicea.service.third.ServiceBFeign;
import com.zjn.servicea.service.third.ServiceCFeign;
import lombok.extern.log4j.Log4j2;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Random;
import java.util.UUID;

/**
 * @author zaker
 * @program: signit_parent
 * @date 2021-03-30 14:28
 */
@Service
public class UserServceImp implements UserService {

    private static final Logger LOG = LoggerFactory.getLogger(UserServceImp.class);

    @Autowired
    private UserDao userDao;

    @Autowired
    private ServiceBFeign serviceBFeign;

    @Autowired
    private ServiceCFeign serviceCFeign;

    @Transactional(rollbackFor=Exception.class)
    @Override
    public String updateUser(User user){
        LOG.info("更新用户表",user);
        int result=userDao.updateByPrimaryKeySelective(user);
        if (result==1){
            return "OK";
        }else
            return "ERROR";
    }

    @Override
    public List<UserInfoReq> getUicInfo(short id, String idNo, Integer offset, Integer limit) {
        LOG.info("获得用户列表");
        List<UserInfoReq> userInfoReqs=userDao.selectInfoUic(id,idNo,offset,limit);
        return  userInfoReqs;
    }

    @Override
    @Transactional
    public String createUserAll(UserInfoReq userInfoReq) {
        LOG.info("创建新用户");
        Short id=(short) new Random().nextInt(100);
        User user=new User();
        user.setId(id);
        user.setName(userInfoReq.getName());
        user.setSex(userInfoReq.getSex());
        user.setIdentityStatus("未认证");
        int userResult=userDao.insertSelective(user);
        Identity identity=new Identity();
        identity.setId(id);
        identity.setIdNo(userInfoReq.getIdNo());
        identity.setRealName(userInfoReq.getName());
        identity.setIdentityStatus("未认证");
        String identityResult=serviceBFeign.createIdentity(identity);
        Contact contact=new Contact();
        contact.setId(id);
        contact.setEmail(userInfoReq.getEmail());
        contact.setName(userInfoReq.getName());
        contact.setPhone(userInfoReq.getPhone());
        String contactResult=serviceCFeign.createContact(contact);
        if (userResult==1&&identityResult.equals("OK")&&contactResult.equals("OK")){
            return "OK";
        }else {
            return "ERROR";
        }

    }


}
